#!/usr/bin/env python
# coding: utf-8

# In[2]:


import pandas as pd
pd.plotting.register_matplotlib_converters()
import matplotlib.pyplot as plt
get_ipython().run_line_magic('matplotlib', 'inline')
import seaborn as sns
import matplotlib.dates as mdates
import numpy as np # linear algebra
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
import math   
from datetime import datetime, date 
import warnings
warnings.filterwarnings('ignore')
from tensorflow import keras
from tensorflow.keras import layers
from keras.models import Sequential
from keras.layers import Dense

print("Setup Complete")


# In[3]:


dam = pd.read_csv("3Dam.csv", encoding= 'cp949')
dam.head()


# In[3]:


dam.columns = ['date', 'water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']
dam.head()


# In[4]:


dam['inflow'] = pd.to_numeric(dam['inflow'],errors='coerce')
dam['outflow'] = pd.to_numeric(dam['outflow'],errors='coerce')
dam['inflow2'] = pd.to_numeric(dam['inflow2'],errors='coerce')
dam['outflow2'] = pd.to_numeric(dam['outflow2'],errors='coerce')
dam['inflow3'] = pd.to_numeric(dam['inflow3'],errors='coerce')
dam['outflow3'] = pd.to_numeric(dam['outflow3'],errors='coerce')


# In[5]:


print(dam.isnull().sum())


# In[6]:


dam = dam.dropna(axis=0)


# In[7]:


print(dam.isnull().sum())


# In[8]:


dam.describe()


# In[9]:


dam=dam[dam['water_level']>100]
dam=dam[dam['rainfall']>=0]
dam=dam[dam['inflow']>=0]
dam=dam[dam['outflow']>=0]

dam=dam[dam['rainfall2']>=0]
dam=dam[dam['inflow2']>=0]
dam=dam[dam['outflow2']>=0]

dam=dam[dam['water_level3']>0]
dam=dam[dam['rainfall3']>=0]
dam=dam[dam['inflow3']>=0]
dam=dam[dam['outflow3']>=0]


# In[10]:


dam.describe()


# In[12]:


dam = dam.reset_index(drop=True)


# # Remove Outlier

# In[13]:


plt.figure(figsize=(25,6))
sns.boxplot(data=dam['outflow'], color='red')
plt.show()


# In[14]:


#remove outlier

Q1 = dam['outflow'].quantile(0.25)
Q3 = dam['outflow'].quantile(0.75)
IQR = Q3 - Q1    #IQR is interquartile range. 

filter = (dam['outflow'] >= Q1 - 1.5 * IQR) & (dam['outflow'] <= Q3 + 1.5 *IQR)
dam = dam.loc[filter]
print(dam['outflow'].describe())
print('\n')
print(dam['outflow'].describe())


# In[15]:


plt.figure(figsize=(25,6))
sns.boxplot(data=dam['outflow'], color='red')
plt.show()


# In[16]:


dam = dam.reset_index(drop=True)


# In[17]:


dam["date"] = pd.to_datetime(dam["date"])


# In[18]:


dam.index = dam["date"]


# In[19]:


dam.head()


# # Normalize

# In[23]:


dam


# In[24]:


features = dam[['water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']]
print(features)


# In[25]:


from sklearn.preprocessing import MinMaxScaler

scaler=MinMaxScaler()
scaler.fit(features)
dam_scaled=scaler.transform(features)

dam_scaled=pd.DataFrame(data=dam_scaled, columns=list(features))
print('feature 최솟값')
print(dam_scaled.min())
print('\n''fearure 최댓값')
print(dam_scaled.max())


# In[26]:


print(dam_scaled)


# In[27]:


dam['variation'] = 0
for i, idx in enumerate(dam.index):
    dam.loc[idx, 'variation'] = dam_scaled['rainfall'][i]+dam_scaled['inflow'][i]-dam_scaled['outflow'][i]


# In[28]:


dam['variation2'] = 0
for i, idx in enumerate(dam.index):
    dam.loc[idx, 'variation2'] = dam_scaled['rainfall2'][i]+dam_scaled['inflow2'][i]-dam_scaled['outflow2'][i]


# In[29]:


dam['variation3'] = 0
for i, idx in enumerate(dam.index):
    dam.loc[idx, 'variation3'] = dam_scaled['rainfall3'][i]+dam_scaled['inflow3'][i]-dam_scaled['outflow3'][i]


# In[30]:


df = dam['variation']
df


# In[31]:


ds_list = []

for i in range(1,len(df)):
    ds = df[i]-df[i-1]
    ds_list.append(ds)
    
ds_dt = pd.DataFrame(ds_list)


# In[32]:


ds_dt.columns = ['ds_dt']
print(ds_dt)


# In[33]:


ds_dt['ds_dt'] = pd.to_numeric(ds_dt['ds_dt'],errors='coerce')
print(ds_dt)


# In[34]:


df2 = dam['variation2']
df2


# In[35]:


ds_list = []

for i in range(1,len(df2)):
    ds = df2[i]-df2[i-1]
    ds_list.append(ds)
    
ds_dt2 = pd.DataFrame(ds_list)


# In[36]:


ds_dt2.columns = ['ds_dt2']
print(ds_dt2)


# In[37]:


ds_dt2['ds_dt2'] = pd.to_numeric(ds_dt2['ds_dt2'],errors='coerce')
print(ds_dt2)


# In[38]:


df3 = dam['variation3']
df3


# In[39]:


ds_list = []

for i in range(1,len(df)):
    ds = df3[i]-df3[i-1]
    ds_list.append(ds)
    
ds_dt3 = pd.DataFrame(ds_list)


# In[40]:


ds_dt3.columns = ['ds_dt3']
print(ds_dt3)


# In[41]:


ds_dt3['ds_dt3'] = pd.to_numeric(ds_dt3['ds_dt3'],errors='coerce')
print(ds_dt3)


# In[42]:


dam = dam[1:]


# In[43]:


dam = dam.reset_index(drop=True)
print(dam)


# In[44]:


dam['ds_dt'] = ds_dt
dam['ds_dt2'] = ds_dt2
dam['ds_dt3'] = ds_dt3


# In[45]:


dam = dam.reset_index(drop=True)
print(dam)


# In[46]:


dam["date"] = pd.to_datetime(dam["date"])


# In[47]:


dam.index = dam["date"]


# # GRU Model

# In[48]:


dam.describe()


# In[49]:


168304*0.8


# In[50]:


train=dam[:134643]
test=dam[134643:168304]


# In[51]:


def make_dataset(data, label, window_size=120):
    feature_list = []
    label_list = []
    for i in range(len(data) - window_size):
        feature_list.append(np.array(data.iloc[i:i+window_size]))
        label_list.append(np.array(label.iloc[i+window_size]))
    return np.array(feature_list), np.array(label_list)


# In[52]:


feature_cols = ['rainfall3', 'inflow3', 'outflow3', 'ds_dt3']
label_cols = ['water_level3']

train_feature = train[feature_cols]
train_label = train[label_cols]

test_feature = test[feature_cols]
test_label = test[label_cols]


# In[53]:


x_train, y_train = make_dataset(train_feature,train_label,120)


# In[54]:


x_test, y_test = make_dataset(test_feature,test_label,120)


# In[55]:


test_feature.shape


# In[259]:


#model_s3d.save("model_s3d.h5")


# In[56]:


from tensorflow.keras.models import load_model
model_s3d = load_model('model_s3d.h5')


# # TRAIN

# In[57]:


plt.figure(figsize=(10,8))
predict_train  = model_s3d.predict(x_train)
y = y_train
plt.plot(y_train, y)
plt.plot(y_train,predict_train, 'ro',alpha = 0.1)
plt.xlabel('Actual water level [EL.m]', fontsize =27)
plt.ylabel('Predicted water level [EL.m]', fontsize =27)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.xlim
plt.ylim
plt.show()


# In[58]:


import hydroeval as he
nse = he.evaluator(he.nse, predict_train, y)
kge, r, alpha, beta = he.evaluator(he.kge, predict_train, y)

print(nse, kge)


# In[270]:


from sklearn.metrics import mean_absolute_error

from sklearn.metrics import mean_squared_error

from sklearn.metrics import mean_squared_log_error

from sklearn.metrics import r2_score

from sklearn.metrics import mean_absolute_percentage_error


# In[271]:


print("MAE : " + str(mean_absolute_error(y, predict_train)))

print("MAPE : " + str(mean_absolute_percentage_error(y, predict_train)))

print("MSE : " + str(mean_squared_error(y, predict_train)))

# RMSE
print("RMSE = ", np.sqrt(mean_squared_error(y, predict_train)))

def rmsle(y, predict_train):
    return np.sqrt(np.mean(np.square(np.log1p(y) - np.log1p(predict_train))))

print("RMSLE = ", rmsle(y, predict_train))

print("R2 : " + str(r2_score(y, predict_train)))


# In[59]:


train=dam[:134643]


# In[60]:


outputs_train2 = pd.DataFrame(y,columns = ['water level'])
outputs_train2['predicted water level'] = predict_train
print(outputs_train2)


# In[61]:


print(type(predict_train))


# In[62]:


predict_train


# In[63]:


oo = dam[120:134643]


# In[64]:


Date=pd.DataFrame(oo.index)


# In[65]:


Date
Date['date'] = pd.to_datetime(Date.date)
print(Date)


# In[66]:


outputs_train3 = pd.concat([Date,outputs_train2],axis = 1)

outputs_train3


# In[67]:


outputs_train3.index = outputs_train3["date"]


# In[68]:


outputs_train3


# In[69]:


plt.figure(figsize=(17, 6))

pre = sns.lineplot(data=outputs_train3)

plt.ylabel('Water level [EL.m]',fontsize = 26)
#plt.xlabel('Date', fontsize =26)
plt.grid(True)
plt.legend(labels=["Actual water level", "Predicted water level"], fontsize=16)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
#pre.xaxis.set_major_locator(mdates.YearLocator())
#pre.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.show()


# In[70]:


rev_normal_tr = pd.DataFrame(predict_train)
rev_normal_tr = rev_normal_tr.ewm(span=11*24).mean()


# In[71]:


outputs_train4 = pd.DataFrame(y,columns = ['water level obs'])
outputs_train4['water level pre'] = rev_normal_tr
print(outputs_train4)


# In[72]:


outputs_train4 = pd.concat([Date,outputs_train4],axis = 1)

outputs_train4.index = outputs_train4["date"]


# In[73]:


plt.figure(figsize=(17, 6))

pre = sns.lineplot(data=outputs_train4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 26)
#plt.xlabel('Date', fontsize =26)
plt.grid(True)
plt.legend(labels=["Actual water level", "Predicted water level"], fontsize=16)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[74]:


plt.figure(figsize=(10,8))
predict_train  = model_s3d.predict(x_train)
y = y_train
plt.plot(y_train, y)
plt.plot(y_train,rev_normal_tr, 'ro',alpha = 0.1)
plt.xlabel('Actual water level [EL.m]', fontsize =27)
plt.ylabel('Predicted water level [EL.m]', fontsize =27)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.xlim
plt.ylim
plt.show()


# In[75]:


import hydroeval as he


# In[76]:


nse = he.evaluator(he.nse, rev_normal_tr, y)
kge, r, alpha, beta = he.evaluator(he.kge, rev_normal_tr, y)

print(nse, kge)


# In[119]:


print("MAE : " + str(mean_absolute_error(y, rev_normal_tr)))

print("MAPE : " + str(mean_absolute_percentage_error(y, rev_normal_tr)))

print("MSE : " + str(mean_squared_error(y, rev_normal_tr)))

# RMSE
print("RMSE = ", np.sqrt(mean_squared_error(y, rev_normal_tr)))

print("R2 : " + str(r2_score(y, rev_normal_tr)))


# In[120]:


#outputs_train4.to_csv('s3t_train.csv')


# In[121]:


#dam.to_csv('dam_s3.csv')


# # TEST

# In[61]:


from tensorflow.keras.models import load_model
model_s3d = load_model('model_s3d.h5')


# In[103]:


plt.figure(figsize=(10,8))
predict_test  = model_s3d.predict(x_test)
y = y_test
plt.plot(y_test, y)
plt.plot(y_test,predict_test, 'ro',alpha = 0.1)
plt.xlabel('Actual water level [EL.m]', fontsize =27)
plt.ylabel('Predicted water level [EL.m]', fontsize =27)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.xlim
plt.ylim
plt.show()


# In[104]:


nse = he.evaluator(he.nse, predict_test, y)
kge, r, alpha, beta = he.evaluator(he.kge, predict_test, y)

print(nse, kge)


# In[105]:


from sklearn.metrics import mean_absolute_error

from sklearn.metrics import mean_squared_error

from sklearn.metrics import mean_squared_log_error

from sklearn.metrics import r2_score

from sklearn.metrics import mean_absolute_percentage_error


# In[106]:


print("MAE : " + str(mean_absolute_error(y, predict_test)))

print("MAPE : " + str(mean_absolute_percentage_error(y, predict_test)))

print("MSE : " + str(mean_squared_error(y, predict_test)))

# RMSE
print("RMSE = ", np.sqrt(mean_squared_error(y, predict_test)))

def rmsle(y, predict_test):
    return np.sqrt(np.mean(np.square(np.log1p(y) - np.log1p(predict_test))))

print("RMSLE = ", rmsle(y, predict_test))

print("R2 : " + str(r2_score(y, predict_test)))


# In[107]:


outputs_test2 = pd.DataFrame(y,columns = ['water level'])
outputs_test2['predicted water level'] = predict_test
print(outputs_test2)


# In[108]:


134643+120


# In[109]:


oo = dam[134763:168304]
Date=pd.DataFrame(oo.index)


# In[110]:


Date=pd.DataFrame(oo.index)


# In[111]:


Date
Date['date'] = pd.to_datetime(Date.date)
print(Date)


# In[112]:


outputs_test2


# In[113]:


outputs_test3 = pd.concat([Date,outputs_test2],axis = 1)

outputs_test3


# In[114]:


outputs_test3.index = outputs_test3["date"]


# In[115]:


outputs_test3


# In[116]:


plt.figure(figsize=(17, 6))

pre = sns.lineplot(data=outputs_test3)

plt.ylabel('Water level [EL.m]',fontsize = 26)
#plt.xlabel('Date', fontsize =26)
plt.grid(True)
plt.legend(labels=["Actual water level", "Predicted water level"], fontsize=16)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
#pre.xaxis.set_major_locator(mdates.YearLocator())
#pre.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))
plt.show()


# In[117]:


#outputs_test3.to_csv('s3_before_ma.csv')


# In[92]:


rev_normal_tr = pd.DataFrame(predict_train)
rev_normal_tr = rev_normal_tr.ewm(span=11*24).mean()


# In[93]:


rev_normal_test = pd.DataFrame(predict_test)
rev_normal_test = rev_normal_test.ewm(span=11*24).mean()


# In[94]:


outputs_test4 = pd.DataFrame(y,columns = ['water level obs'])
outputs_test4['water level pre'] = rev_normal_test
print(outputs_test4)


# In[95]:


outputs_test4 = pd.concat([Date,outputs_test4],axis = 1)

outputs_test4.index = outputs_test4["date"]


# In[96]:


outputs_test4


# In[97]:


plt.figure(figsize=(17, 6))

pre = sns.lineplot(data=outputs_test4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 26)
#plt.xlabel('Date', fontsize =26)
plt.grid(True)
plt.legend(labels=["Actual water level", "Predicted water level"], fontsize=16)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[98]:


plt.figure(figsize=(10,8))
predict_test  = model_s3d.predict(x_test)
y = y_test
plt.plot(y_test, y)
plt.plot(y_test,rev_normal_test, 'ro',alpha = 0.1)
plt.xlabel('Actual water level [EL.m]', fontsize =27)
plt.ylabel('Predicted water level [EL.m]', fontsize =27)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.xlim
plt.ylim
plt.show()


# In[99]:


import hydroeval as he


# In[102]:


nse = he.evaluator(he.nse, rev_normal_test, y)
kge, r, alpha, beta = he.evaluator(he.kge, rev_normal_test, y)

print(nse, kge)


# In[237]:


print("MAE : " + str(mean_absolute_error(y, rev_normal_test)))

print("MAPE : " + str(mean_absolute_percentage_error(y, rev_normal_test)))

print("MSE : " + str(mean_squared_error(y, rev_normal_test)))

# RMSE
print("RMSE = ", np.sqrt(mean_squared_error(y, rev_normal_test)))

print("R2 : " + str(r2_score(y, rev_normal_test)))


# In[100]:


#outputs_test4.to_csv('s3t.csv')


# # Scenario

# # Outflow

# In[441]:


first=dam[134643:168304]


# In[442]:


first


# In[443]:


first['outflow3'] = first['outflow3']*1.4


# In[444]:


features = first[['water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']]
print(features)


# In[445]:


from sklearn.preprocessing import MinMaxScaler

scaler=MinMaxScaler()
scaler.fit(features)
dam_scaled=scaler.transform(features)

dam_scaled=pd.DataFrame(data=dam_scaled, columns=list(features))
print('feature 최솟값')
print(dam_scaled.min())
print('\n''fearure 최댓값')
print(dam_scaled.max())


# In[446]:


first['variation3'] = 0
for i, idx in enumerate(first.index):
    first.loc[idx, 'variation3'] = dam_scaled['rainfall3'][i]+dam_scaled['inflow3'][i]-dam_scaled['outflow3'][i]


# In[447]:


df2 = first['variation3']
df2


# In[448]:


df2 = df2.reset_index(drop=True)


# In[449]:


ds_list = []

for i in range(1,len(df2)):
    ds = df2[i]-df2[i-1]
    ds_list.append(ds)
    
ds_dt3 = pd.DataFrame(ds_list)


# In[450]:


ds_dt3.columns = ['ds_dt3']
print(ds_dt3)


# In[451]:


ds_dt3['ds_dt3'] = pd.to_numeric(ds_dt3['ds_dt3'],errors='coerce')
print(ds_dt3)


# In[452]:


first = first[1:]


# In[453]:


first = first.reset_index(drop=True)


# In[454]:


first['ds_dt3'] = ds_dt3


# In[455]:


first = first.reset_index(drop=True)


# In[456]:


orginal_out1= predict_test[1:]


# In[457]:


train_feature1 = first[feature_cols]
train_label1 = first[label_cols]


# In[458]:


x_rcheck1, y_rcheck1 = make_dataset(train_feature1,train_label1,120)


# In[459]:


sin_sm2a = model_s3d.predict(x_rcheck1)


# In[460]:


obs = dam.water_level3[134764:168304].to_numpy()


# In[461]:


obs = pd.DataFrame(obs,columns = ['observation']) 
obs['original prediction'] = orginal_out1
obs['Scenario : 0.8 outflow'] = sin_sm2a


# In[462]:


outputs_plot1 = pd.DataFrame(obs) #plot2,t2


# In[463]:


outputs_plot1


# In[464]:


oo = dam[134764:168304]


# In[465]:


Date=pd.DataFrame(oo.index)


# In[466]:


outputs_plot1 = pd.concat([Date,obs],axis = 1)


# In[467]:


outputs_plot1.index = outputs_plot1["date"]


# In[468]:


outputs_plot1


# In[469]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_plot1)#, palette=['dodgerblue', 'orange', 'red'])

plt.ylabel('Water Level [EL.m]', fontsize =25)
plt.xlim
plt.ylim
plt.grid(True)  
plt.legend(fontsize =19)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[470]:


original_pre_169 = pd.DataFrame(orginal_out1)
original_pre_169 = original_pre_169.ewm(span=721).mean()


# In[471]:


sin1_169 = pd.DataFrame(sin_sm2a)
sin1_169 = sin1_169.ewm(span=721).mean()


# In[472]:


obs['original prediction'] = original_pre_169
obs['Scenario : 0.8 outflow'] = sin1_169
print(obs)


# In[473]:


outputs_test4 = pd.concat([Date,obs],axis = 1)

outputs_test4


# In[474]:


outputs_test4.index = outputs_test4["date"]


# In[475]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_test4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 25)
plt.xlabel('Date', fontsize =17)
plt.title('Juam Control dam\n', fontsize =25)
plt.grid(True)  
plt.legend(fontsize =18)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[476]:


#outputs_test4.to_csv('s3t_0.8.csv')


# # Inflow

# In[187]:


first=dam[134643:168304]


# In[188]:


first['inflow3'] = first['inflow3']*0.6


# In[189]:


features = first[['water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']]
print(features)


# In[190]:


from sklearn.preprocessing import MinMaxScaler

scaler=MinMaxScaler()
scaler.fit(features)
dam_scaled=scaler.transform(features)

dam_scaled=pd.DataFrame(data=dam_scaled, columns=list(features))
print('feature 최솟값')
print(dam_scaled.min())
print('\n''fearure 최댓값')
print(dam_scaled.max())


# In[191]:


first['variation3'] = 0
for i, idx in enumerate(first.index):
    first.loc[idx, 'variation3'] = dam_scaled['rainfall3'][i]+dam_scaled['inflow3'][i]-dam_scaled['outflow3'][i]


# In[192]:


df2 = first['variation3']
df2


# In[193]:


df2 = df2.reset_index(drop=True)


# In[194]:


ds_list = []

for i in range(1,len(df2)):
    ds = df2[i]-df2[i-1]
    ds_list.append(ds)
    
ds_dt3 = pd.DataFrame(ds_list)


# In[195]:


ds_dt3.columns = ['ds_dt3']
print(ds_dt3)


# In[196]:


ds_dt3['ds_dt3'] = pd.to_numeric(ds_dt3['ds_dt3'],errors='coerce')
print(ds_dt3)


# In[197]:


first = first[1:]


# In[198]:


first = first.reset_index(drop=True)


# In[199]:


first


# In[200]:


first['ds_dt3'] = ds_dt3


# In[201]:


first = first.reset_index(drop=True)


# In[203]:


first


# In[202]:


orginal_out1= predict_test[1:]


# In[205]:


train_feature1 = first[feature_cols]
train_label1 = first[label_cols]


# In[206]:


x_rcheck1, y_rcheck1 = make_dataset(train_feature1,train_label1,120)


# In[207]:


sin_sm2a = model_s3d.predict(x_rcheck1)


# In[208]:


obs = dam.water_level3[134764:168304].to_numpy()


# In[209]:


obs = pd.DataFrame(obs,columns = ['observation']) 
obs['original prediction'] = orginal_out1
obs['Scenario : 0.6 Inflow'] = sin_sm2a


# In[210]:


outputs_plot1 = pd.DataFrame(obs) #plot2,t2


# In[211]:


outputs_plot1


# In[212]:


oo = dam[134764:168304]


# In[213]:


Date=pd.DataFrame(oo.index)


# In[214]:


outputs_plot1 = pd.concat([Date,obs],axis = 1)


# In[215]:


outputs_plot1.index = outputs_plot1["date"]


# In[216]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_plot1)#, palette=['dodgerblue', 'orange', 'red'])

plt.ylabel('Water Level [EL.m]', fontsize =25)
plt.xlim
plt.ylim
plt.grid(True)  
plt.legend(fontsize =19)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[217]:


original_pre_169 = pd.DataFrame(orginal_out1)
original_pre_169 = original_pre_169.ewm(span=721).mean()


# In[218]:


sin1_169 = pd.DataFrame(sin_sm2a)
sin1_169 = sin1_169.ewm(span=721).mean()


# In[219]:


obs['original prediction'] = original_pre_169
obs['Scenario : 0.6 Inflow'] = sin1_169
print(obs)


# In[220]:


outputs_test4 = pd.concat([Date,obs],axis = 1)

outputs_test4


# In[221]:


outputs_test4.index = outputs_test4["date"]


# In[222]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_test4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 25)
plt.xlabel('Date', fontsize =17)
plt.title('Juam Control dam\n', fontsize =25)
plt.grid(True)  
plt.legend(fontsize =18)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[223]:


#outputs_test4.to_csv('s3t_0.6_inflow.csv')


# # Rainfall

# In[323]:


first=dam[134643:168304]


# In[324]:


first['rainfall3'] = first['rainfall3']*0.6


# In[325]:


features = first[['water_level', 'rainfall', 'inflow', 'outflow', 'water_level2', 'rainfall2', 'inflow2', 'outflow2','water_level3', 'rainfall3', 'inflow3', 'outflow3']]
print(features)


# In[326]:


from sklearn.preprocessing import MinMaxScaler

scaler=MinMaxScaler()
scaler.fit(features)
dam_scaled=scaler.transform(features)

dam_scaled=pd.DataFrame(data=dam_scaled, columns=list(features))
print('feature 최솟값')
print(dam_scaled.min())
print('\n''fearure 최댓값')
print(dam_scaled.max())


# In[327]:


first['variation3'] = 0
for i, idx in enumerate(first.index):
    first.loc[idx, 'variation3'] = dam_scaled['rainfall3'][i]+dam_scaled['inflow3'][i]-dam_scaled['outflow3'][i]


# In[328]:


df2 = first['variation3']
df2


# In[329]:


df2 = df2.reset_index(drop=True)


# In[330]:


ds_list = []

for i in range(1,len(df2)):
    ds = df2[i]-df2[i-1]
    ds_list.append(ds)
    
ds_dt3 = pd.DataFrame(ds_list)


# In[331]:


ds_dt3.columns = ['ds_dt3']
print(ds_dt3)


# In[332]:


ds_dt3['ds_dt3'] = pd.to_numeric(ds_dt3['ds_dt3'],errors='coerce')
print(ds_dt3)


# In[333]:


first = first[1:]


# In[334]:


first = first.reset_index(drop=True)


# In[335]:


first['ds_dt3'] = ds_dt3


# In[336]:


first = first.reset_index(drop=True)


# In[337]:


orginal_out1= predict_test[1:]


# In[338]:


train_feature1 = first[feature_cols]
train_label1 = first[label_cols]


# In[339]:


x_rcheck1, y_rcheck1 = make_dataset(train_feature1,train_label1,120)


# In[340]:


sin_sm2a = model_s3d.predict(x_rcheck1)


# In[341]:


obs = dam.water_level3[134764:168304].to_numpy()


# In[342]:


obs = pd.DataFrame(obs,columns = ['observation']) 
obs['original prediction'] = orginal_out1
obs['Scenario : 0.6 Rainfall'] = sin_sm2a


# In[343]:


outputs_plot1 = pd.DataFrame(obs) #plot2,t2


# In[344]:


oo = dam[134764:168304]


# In[345]:


Date=pd.DataFrame(oo.index)


# In[346]:


outputs_plot1 = pd.concat([Date,obs],axis = 1)


# In[347]:


outputs_plot1.index = outputs_plot1["date"]


# In[348]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_plot1)#, palette=['dodgerblue', 'orange', 'red'])

plt.ylabel('Water Level [EL.m]', fontsize =25)
plt.xlim
plt.ylim
plt.grid(True)  
plt.legend(fontsize =19)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[349]:


original_pre_169 = pd.DataFrame(orginal_out1)
original_pre_169 = original_pre_169.ewm(span=721).mean()


# In[350]:


sin1_169 = pd.DataFrame(sin_sm2a)
sin1_169 = sin1_169.ewm(span=721).mean()


# In[351]:


obs['original prediction'] = original_pre_169
obs['Scenario : 0.6 Rainfall'] = sin1_169
print(obs)


# In[352]:


outputs_test4 = pd.concat([Date,obs],axis = 1)

outputs_test4


# In[353]:


outputs_test4.index = outputs_test4["date"]


# In[354]:


plt.figure(figsize=(20, 6))

pre = sns.lineplot(data=outputs_test4, linewidth=3)

plt.ylabel('Water level [EL.m]',fontsize = 25)
plt.xlabel('Date', fontsize =17)
plt.title('Juam Control dam\n', fontsize =25)
plt.grid(True)  
plt.legend(fontsize =18)
plt.xticks(fontsize =18)
plt.yticks(fontsize =18)
plt.show()


# In[355]:


#outputs_test4.to_csv('s3t_0.6_rainfall.csv')


# In[ ]:




